//
//  WmcShareAlertView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/27.
//

import UIKit
import YPBedrock
import YPUIKit

extension WmcShareAlertView {
    /// 分享类型
    enum ShareType {
        /// 文字
        case text(config: Config)

        /// 图片
        case image(_ image: UIImage, config: Config)

        /// 链接
        case link(_ link: String, config: Config)

        /// 文件
        case file(_ path: String, fileExtension: String, config: Config)

        struct Config {
            // Lifecycle

            init(title: String? = nil,
                 description: String? = nil,
                 thumbImage: UIImage? = nil,
                 completion: YPBlock.OneParam<Bool>? = nil)
            {
                self.title = title
                self.description = description
                self.thumbImage = thumbImage
                self.completion = completion
            }

            // Internal

            var title: String?
            var description: String?
            var thumbImage: UIImage?
            var completion: YPBlock.OneParam<Bool>?
        }
    }
}

// MARK: - [WmcShareAlertView]

class WmcShareAlertView: WmcSelectView {
    /// 分享类型
    var shareType: ShareType?

    lazy var stackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [
            wechatBtn,
            circleOfFriendsBtn,
        ])
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        return stackView
    }()

    lazy var wechatBtn: WmcShareButton = {
        let btn = WmcShareButton(title: "微信", image: "wechat_icon_color".wmc_image, color: .wmc_green_E6F8E6)
        return btn
    }()

    lazy var circleOfFriendsBtn: WmcShareButton = {
        let btn = WmcShareButton(title: "朋友圈", image: "friends_circle_color".wmc_image, color: .wmc_grayH22_FFF5E7)
        return btn
    }()

    lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage("close".wmc_image, for: .normal)
        return btn
    }()

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "选择分享方式"
        label.textAlignment = .center
        label.textColor = .wmc_grayH2_323233
        label.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        return label
    }()

    override func makeUI() {
        super.makeUI()
        titleView.isHidden = true

        addSubview(closeBtn)
        addSubview(titleLabel)
        addSubview(stackView)

        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.top.equalTo(16)
            make.width.height.equalTo(20)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(40)
            make.left.right.equalToSuperview()
        }

        let bottom = 40 + Wmc_safeAreaBottom
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(90)
            make.bottom.equalTo(-bottom)
        }
    }

    override func bindViewAction() {
        titleView.leftBtn.yp.addEvent { _ in
            YPApp.CurrentVC?.dismiss(animated: true)
        }

        closeBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        wechatBtn.clickClosure = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.share(scene: .session)
        }

        circleOfFriendsBtn.clickClosure = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.share(scene: .friendCircle)
        }
    }

    func share(scene: WmcShareManager.Scene) {
        guard let shareType = shareType else { return }
        switch shareType {
        case let .text(config):
            WmcShareManager.sendText(config.title ?? "",
                                     description: config.description,
                                     scene: scene,
                                     completion: config.completion)
        case let .image(image, config):
            WmcShareManager.shareImage(image,
                                       title: config.title,
                                       description: config.description,
                                       messageExt: nil,
                                       scene: scene,
                                       completion: config.completion)
        case let .link(link, config):
            WmcShareManager.shareLink(link,
                                      title: config.title ?? "",
                                      description: config.description,
                                      thumbImage: config.thumbImage,
                                      scene: scene,
                                      completion: config.completion)
        case let .file(path, fileExtension, config):
            WmcShareManager.shareFile(path,
                                      fileExtension: fileExtension,
                                      title: config.title ?? "",
                                      description: config.description,
                                      thumbImage: config.thumbImage,
                                      scene: scene,
                                      completion: config.completion)
        }
    }

    func dismiss() {
        YPApp.CurrentVC?.dismiss(animated: true)
    }
}


extension WmcShareAlertView: YPUIPresentTarget {
    /// 显示
    func show() -> Void {
        let config = YPUIPresentController.Config()
        config.contentView = self
        config.transitionAnimated.usingSpringWithDamping = .init(appearDuration: 1, disappearDuration: 1)
        config.transitionAnimated.initialSpringVelocity = .init(appearDuration: 0, disappearDuration: 0)
        let height = Wmc_safeAreaBottom + 214
        config.style = .sheet(.Create({ make in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(height)
        }))
        present(config: config)
    }
}
